package org.example.day20240904.Scott;

import org.example.day20240904.JDBCUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ScottMain {
    public static void main(String[] args) throws SQLException, IOException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from emp";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        FileWriter fileWriter = new FileWriter("emp.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        while (resultSet.next()){
            bufferedWriter.write(resultSet.getInt("EMPNO") + ","
                                + resultSet.getString("ENAME") + ","
                                + resultSet.getString("JOB") + ","
                                + resultSet.getInt("MGR") + ","
                                + resultSet.getString("HIREDATE") + ","
                                + resultSet.getDouble("SAL") + ","
                                + resultSet.getDouble("COMM") + ","
                                + resultSet.getInt("deptno")
                                );
            bufferedWriter.write("\n");
        }
        bufferedWriter.close();
        fileWriter.close();
        JDBCUtils.close(connection,preparedStatement,resultSet);
    }
}
